<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>好友留言板</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #1a2a6c, #2c3e50);
            min-height: 100vh;
            padding: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .container {
            max-width: 800px;
            width: 100%;
            margin: 0 auto;
            background: rgba(255, 255, 255, 0.08);
            backdrop-filter: blur(12px);
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
            overflow: hidden;
            border: 1px solid rgba(255, 255, 255, 0.1);
        }

        .header {
            background: rgba(0, 0, 0, 0.2);
            color: white;
            padding: 30px;
            text-align: center;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
            text-shadow: 0 2px 4px rgba(0,0,0,0.3);
        }

        .header p {
            opacity: 0.9;
            font-size: 1.1em;
        }

        .message-form {
            padding: 30px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: rgba(255, 255, 255, 0.85);
        }

        .form-group input,
        .form-group textarea {
            width: 100%;
            padding: 12px 15px;
            background: rgba(0, 0, 0, 0.15);
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 10px;
            font-size: 16px;
            transition: all 0.3s ease;
            color: white;
        }

        .form-group input:focus,
        .form-group textarea:focus {
            outline: none;
            border-color: #00c9ff;
            box-shadow: 0 0 0 3px rgba(0, 201, 255, 0.2);
        }

        .form-group textarea {
            resize: vertical;
            min-height: 100px;
        }

        .submit-btn {
            background: linear-gradient(135deg, #00c9ff, #92fe9d);
            color: #1a2a6c;
            border: none;
            padding: 14px 35px;
            font-size: 17px;
            font-weight: 600;
            border-radius: 50px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }

        .submit-btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
        }

        .messages-container {
            padding: 30px;
            max-height: 500px;
            overflow-y: auto;
        }

        .message-item {
            background: rgba(255, 255, 255, 0.1);
            border-radius: 15px;
            padding: 20px;
            margin-bottom: 20px;
            position: relative;
            animation: slideIn 0.5s ease-out;
            border: 1px solid rgba(255, 255, 255, 0.1);
            transition: transform 0.3s ease;
        }

        .message-item:hover {
            transform: translateY(-5px);
            background: rgba(255, 255, 255, 0.15);
        }

        @keyframes slideIn {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .message-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 12px;
        }

        .message-author {
            font-weight: bold;
            color: #00c9ff;
            font-size: 1.1em;
        }

        .message-time {
            color: rgba(255, 255, 255, 0.6);
            font-size: 14px;
        }

        .message-content {
            color: rgba(255, 255, 255, 0.85);
            line-height: 1.6;
            white-space: pre-wrap;
            word-wrap: break-word;
        }

        .delete-btn {
            position: absolute;
            top: 15px;
            right: 15px;
            background: rgba(255, 107, 107, 0.8);
            color: white;
            border: none;
            padding: 6px 12px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 13px;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .delete-btn:hover {
            background: rgba(255, 107, 107, 1);
            transform: scale(1.05);
        }

        .empty-state {
            text-align: center;
            padding: 40px;
            color: rgba(255, 255, 255, 0.5);
        }

        .empty-state img {
            width: 100px;
            opacity: 0.5;
            margin-bottom: 20px;
        }

        .stats {
            background: rgba(0, 0, 0, 0.15);
            padding: 15px;
            text-align: center;
            color: rgba(255, 255, 255, 0.7);
            border-top: 1px solid rgba(255, 255, 255, 0.1);
        }

        @media (max-width: 600px) {
            .header h1 {
                font-size: 2em;
            }
            
            .message-form,
            .messages-container {
                padding: 20px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>💬 好友留言板</h1>
            <p>留下你的足迹，分享你的心情</p>
        </div>

        <div class="message-form">
            <form id="messageForm">
                <div class="form-group">
                    <label for="authorName">你的名字</label>
                    <input type="text" id="authorName" placeholder="请输入你的名字" required>
                </div>
                <div class="form-group">
                    <label for="messageContent">留言内容</label>
                    <textarea id="messageContent" placeholder="写下你想说的话..." required></textarea>
                </div>
                <button type="submit" class="submit-btn">发表留言</button>
            </form>
        </div>

        <div class="messages-container" id="messagesContainer">
            <div class="empty-state" id="emptyState">
                <p>还没有留言，快来抢沙发吧！</p>
            </div>
        </div>

        <div class="stats" id="stats">
            共 <span id="messageCount">0</span> 条留言
        </div>
    </div>

    <script>
        // 获取DOM元素
        const messageForm = document.getElementById('messageForm');
        const authorNameInput = document.getElementById('authorName');
        const messageContentInput = document.getElementById('messageContent');
        const messagesContainer = document.getElementById('messagesContainer');
        const emptyState = document.getElementById('emptyState');
        const messageCountSpan = document.getElementById('messageCount');

        // 从本地存储加载留言
        let messages = JSON.parse(localStorage.getItem('messages')) || [];

        // 初始化
        function init() {
            renderMessages();
            updateStats();
        }

        // 渲染留言列表
        function renderMessages() {
            if (messages.length === 0) {
                emptyState.style.display = 'block';
                return;
            }

            emptyState.style.display = 'none';
            messagesContainer.innerHTML = '';

            messages.forEach((message, index) => {
                const messageItem = createMessageElement(message, index);
                messagesContainer.appendChild(messageItem);
            });
        }

        // 创建留言元素
        function createMessageElement(message, index) {
            const div = document.createElement('div');
            div.className = 'message-item';
            div.innerHTML = `
                <div class="message-header">
                    <span class="message-author">${escapeHtml(message.author)}</span>
                    <span class="message-time">${formatTime(message.time)}</span>
                </div>
                <div class="message-content">${escapeHtml(message.content)}</div>
                <button class="delete-btn" onclick="deleteMessage(${index})">删除</button>
            `;
            return div;
        }

        // 转义HTML特殊字符
        function escapeHtml(text) {
            const map = {
                '&': '&amp;',
                '<': '&lt;',
                '>': '&gt;',
                '"': '&quot;',
                "'": '&#039;'
            };
            return text.replace(/[&<>"']/g, m => map[m]);
        }

        // 格式化时间
        function formatTime(timestamp) {
            const date = new Date(timestamp);
            const now = new Date();
            const diff = now - date;

            if (diff < 60000) {
                return '刚刚';
            } else if (diff < 3600000) {
                return Math.floor(diff / 60000) + '分钟前';
            } else if (diff < 86400000) {
                return Math.floor(diff / 3600000) + '小时前';
            } else {
                return date.toLocaleDateString('zh-CN');
            }
        }

        // 添加留言
        function addMessage(event) {
            event.preventDefault();

            const author = authorNameInput.value.trim();
            const content = messageContentInput.value.trim();

            if (!author || !content) {
                alert('请填写完整信息！');
                return;
            }

            const newMessage = {
                author: author,
                content: content,
                time: new Date().toISOString()
            };

            messages.unshift(newMessage);
            localStorage.setItem('messages', JSON.stringify(messages));

            // 清空表单
            messageContentInput.value = '';
            
            // 重新渲染
            renderMessages();
            updateStats();

            // 滚动到顶部
            messagesContainer.scrollTop = 0;
        }

        // 删除留言
        function deleteMessage(index) {
            if (confirm('确定要删除这条留言吗？')) {
                messages.splice(index, 1);
                localStorage.setItem('messages', JSON.stringify(messages));
                renderMessages();
                updateStats();
            }
        }

        // 更新统计
        function updateStats() {
            messageCountSpan.textContent = messages.length;
        }

        // 绑定表单提交事件
        messageForm.addEventListener('submit', addMessage);

        // 初始化页面
        init();
    </script>
</body>
</html>